
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * @author CULUN
 */
public class Main extends MIDlet {

    private Display iDisplay;
    private int nHighScore = 3;
    private int iHighScore;
    private boolean iFirstStart = true;
    private PlayGame iPlayGame;
    private MenuScreen iMenuScreen;
    private HighScoreScreen iHighScoreScreen;
    private LoadGameScreen iLoadGameScreen;
    private RecordStore rsHighScore;
    private int[] arScore;
    private String[] arNickName;
    private int[] arIndex;
    private int indexBot;

    public Main() {
        //iHighScoreScreen = new HighScoreScreen(this, iMenuScreen);
        arScore = new int[100];
        arNickName = new String[100];
        arIndex = new int[100];
    }

    public void startApp() {
        if (iFirstStart) {
            try {
                openRecord();
            } catch (RecordStoreException ex) {
            }
            iDisplay = Display.getDisplay(this);

            /*               iLoadGameScreen = new LoadGameScreen(this);
            iLoadGameScreen.Start();
            iDisplay.setCurrent(iLoadGameScreen);   
            
             * 
             */

            iMenuScreen = new MenuScreen(this);
            iMenuScreen.Start();
            iDisplay.setCurrent(iMenuScreen);


            iFirstStart = false;
        }
    }

    public int addScore(String location, String score) throws IOException, RecordStoreException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            dos.writeUTF(location);
            dos.writeUTF(score);
        } catch (IOException ex) {
        }
        this.rsHighScore.addRecord(baos.toByteArray(), 0, baos.toByteArray().length);
        return 1;
    }

    public void deleteScore() throws RecordStoreException {
        rsHighScore.deleteRecord(indexMin());
    }

    public void openRecord() throws RecordStoreException {
        rsHighScore = RecordStore.openRecordStore("HighScore", true);
    }

    public RecordStore getRecordStore() {
        return rsHighScore;
    }

    public void doRaMang() throws RecordStoreNotOpenException, RecordStoreException, IOException {
        byte[] b = null;
        int j = 1, i = 1;
        arIndex[0] = 0;
        int n = rsHighScore.getNumRecords();
        for (i = 1; i <= n;) {
            b = rsHighScore.getRecord(j);
            System.out.println("....");
            j++;
            if (b == null) {
                continue;
            }
            arIndex[i - 1] = j - 1;
            ByteArrayInputStream bin = new ByteArrayInputStream(b);
            DataInputStream din = new DataInputStream(bin);

            String iNickName = din.readUTF();
            arNickName[i - 1] = iNickName;
            String iScore = din.readUTF();
            arScore[i - 1] = Integer.parseInt(iScore);

            i++;
        }
        iHighScore = i - 1;
    }

    public int indexMin() {
        try {
            doRaMang();
        } catch (RecordStoreException ex) {
        } catch (IOException ex) {
        }
        int lc = 0;
        for (int i = 0; i < iHighScore; i++) {
            if (arScore[i] <= arScore[lc]) {
                lc = i;
            }
        }
        return arIndex[lc];
    }

    public void sapXepDiem() {
        for (int i = 0; i < iHighScore - 1; i++) {
            for (int j = i + 1; j < iHighScore; j++) {
                if (arScore[j] > arScore[i]) {
                    int tam = arScore[i];
                    String str = arNickName[i];
                    arScore[i] = arScore[j];
                    arNickName[i] = arNickName[j];
                    arScore[j] = tam;
                    arNickName[j] = str;
                }
            }
        }
    }

    public int ktDuocLuu(String score) {
        try {
            doRaMang();
        } catch (RecordStoreException ex) {
        } catch (IOException ex) {
        }
        sapXepDiem();
        if (iHighScore < nHighScore) {
            return 1;
        } else {
            if (Integer.parseInt(score) > arScore[nHighScore - 1]) {
                return 2;
            }
        }
        return 0;
    }

    public String[] getArNickName() {
        return arNickName;
    }

    public int[] getArScore() {
        return arScore;
    }

    public int get_nHighScore() {
        return nHighScore;
    }
    
    public int get_iHighScore() {
        return iHighScore;
    }


    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        if (iPlayGame != null) {
            iPlayGame.stop();
        }
    }

    public void exit() {
        destroyApp(true);
        notifyDestroyed();
    }
}
